package com.zoe.onelink.business.filter;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zoe.onelink.business.constant.RequestConst;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.TimeZone;


/**
 * <p>标题: 包装参数过滤器</p>
 * <p>描述: 将api默认的parameter参数转换后重新放进请求中</p>
 * <p>版权: Copyright (c) 2016</p>
 * <p>公司: </p>
 *
 * @version: 1.0
 * @author: ffs
 */
public class WrapParameterFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        String parameter = request.getParameter(RequestConst.PARAM_BASE);
        if (StrUtil.isBlank(parameter)) {
            chain.doFilter(request, response);
        } else {
            HashMap requestParam = JSON.parseObject(parameter, HashMap.class);
            ApiHttpRequestWrapper requestWrapper = new ApiHttpRequestWrapper((HttpServletRequest) request, requestParam, true);
            chain.doFilter(requestWrapper, response);
        }
    }

    @Override
    public void destroy() {

    }
}
